Skip to content

Boolean Expressions and if Statements

Boolean Expressions

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Evaluate Boolean expressions that use relational operators in program code.

ESSENTIAL KNOWLEDGE

  • Primitive values and reference values can be compared using relational operators (i.e., == and !=).

  • Arithmetic expression values can be compared using relational operators (i.e., <, >, <=, >=).

  • An expression involving relational operators evaluates to a Boolean value.

if Statements and Control Flow

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent branching logical processes by using conditional statements.

ESSENTIAL KNOWLEDGE

  • Conditional statements interrupt the sequential execution of statements.

  • if statements affect the flow of control by executing different statements based on the value of a Boolean expression.

  • A one-way selection (if statement) is written when there is a set of statements to execute under a certain condition. In this case, the body is executed only when the Boolean condition is true.

if-else Statements

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent branching logical processes by using conditional statements.

ESSENTIAL KNOWLEDGE

  • A two-way selection is written when there are two sets of statements— one to be executed when the Boolean condition is true, and another set for when the Boolean condition is false. In this case, the body of the “if” is executed when the Boolean condition is true, and the body of the “else” is executed when the Boolean condition is false.

else if Statements

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent branching logical processes by using conditional statements.

ESSENTIAL KNOWLEDGE

  • A multi-way selection is written when there are a series of conditions with different statements for each condition. Multi-way selection is performed using if-else-if statements such that exactly one section of code is executed based on the first condition that evaluates to true.

Compound Boolean Expressions

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent branching logical processes by using nested conditional statements.

ESSENTIAL KNOWLEDGE

  • Nested if statements consist of if statements within if statements.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Evaluate compound Boolean expressions in program code.

ESSENTIAL KNOWLEDGE

  • Logical operators !(not), &&(and), and ||(or) are used with Boolean values. This represents the order these operators will be evaluated.

  • An expression involving logical operators evaluates to a Boolean value.

  • When the result of a logical expression using && or || can be determined by evaluating only the first Boolean operand, the second is not evaluated. This is known as short-circuited evaluation.

Equivalent Boolean Expressions

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Compare and contrast equivalent Boolean expressions.

ESSENTIAL KNOWLEDGE

  • De Morgan’s Laws can be applied to Boolean expressions.

  • Truth tables can be used to prove Boolean identities.

  • Equivalent Boolean expressions will evaluate to the same value in all cases.

Comparing Objects

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Compare object references using Boolean expressions in program code.

ESSENTIAL KNOWLEDGE

  • Two object references are considered aliases when they both reference the same object.

  • Object reference values can be compared, using == and !=, to identify aliases.

  • A reference value can be compared with null, using == or !=, to determine if the reference actually references an object.

  • Often classes have their own equals method, which can be used to determine whether two objects of the class are equivalent.